package com.zwy.rxjava;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * 使用Action
 */
public class RxJavaDemo2 extends Activity implements OnClickListener {
	
	private TextView tvs;
	private TextView tve;
	private TextView tvc;
	private Button btn1;
	private Button btn2;
	private Button btn3;
	
	private Action1<MessageEvent> onNextAction;
	//private Action1<Throwable> onErrorAction;
	private Action0 onCompleteAction;
	private Action1<Throwable> onErrorAction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_rxjava2);
		
		tvs = (TextView) findViewById(R.id.tvs_rx2);
		tve = (TextView) findViewById(R.id.tve_rx2);
		tvc = (TextView) findViewById(R.id.tvc_rx2);
		
		btn1 = (Button) findViewById(R.id.btn1_rx2);
		btn2 = (Button) findViewById(R.id.btn2_rx2);
		btn3 = (Button) findViewById(R.id.btn3_rx2);
		
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		
		//发送消息4按钮
		findViewById(R.id.btn4_rx2).setOnClickListener(this);;
		//onNext回调
		/*onNextAction = new Action1<Message>() {
			@Override
			public void call(Message msg) {
				tvs.setText(msg.getMsg());
			}
		};*/
		
		onNextAction = new Action1<MessageEvent>() {

			@Override
			public void call(MessageEvent arg0) {
				tvs.setText(arg0.getMsg());
			}
		};
		
		
		//onError回调
		/*onErrorAction = new Action1<Throwable>() {
			@Override
			public void call(Throwable arg0) {
				tve.setText(arg0.getMessage());
			}
		};*/ 
		
		onErrorAction = new Action1<Throwable>() {

			@Override
			public void call(Throwable arg0) {
				tve.setText(arg0.getMessage());
			}
		};
		
		//onCompleted回调
		onCompleteAction = new Action0() {
			@Override
			public void call() {
				tvc.setText("完成!!");
			}
		};
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1_rx2:
			/**
			 * just操作符+Action
			 */
			//发送消息
			Observable<MessageEvent> myObservable = Observable.just(new MessageEvent("Hello World"));
			//消费消息
			myObservable.subscribe(onNextAction,onErrorAction,onCompleteAction); 
			
			break;
		case R.id.btn2_rx2:
			/**
			 * just操作符+Action
			 */
			Observable<MessageEvent> myObservable2 = Observable.just(new MessageEvent("世界和平"));
			//也可以只关注onNext事件
			myObservable2.subscribe(onNextAction);
			
			
			break;
		case R.id.btn3_rx2:
			
			Observable.just(new MessageEvent("简单写法111！")).subscribe(new Action1<MessageEvent>() {

				@Override
				public void call(MessageEvent arg0) {
					tvs.setText(arg0.getMsg());
				}
			});
			
			//原始代码:
			/*Observable.just(new Message("简单写法！！！！！不错哦！！")).subscribe(new Action1<Message>() {
				@Override
				public void call(Message arg0) {
					tvs.setText(arg0.getMsg());
				}
			});*/
			break;
			
		case R.id.btn4_rx2:
			
			//itchen手写代码内容.
			Observable.just(new MessageEvent("itchen第四种写法的变形-->")).subscribe(new Subscriber<MessageEvent>() {

				@Override
				public void onCompleted() {
				}

				@Override
				public void onError(Throwable arg0) {
				}

				@Override
				public void onNext(MessageEvent arg0) {
					tvs.setText(arg0.getMsg()+"测试测试");
				}
			}) ;
			
			/**
			 * just操作符+Subscriber
			 */
			//原始代码:
			/*Observable.just(new Message("Hello World!fuck!!第四种写法操作.")).subscribe(new Subscriber<Message>() {
				@Override
				public void onCompleted() {
					
				}
				@Override
				public void onError(Throwable arg0) {
					
				}
				@Override
				public void onNext(Message arg0) {
					tvs.setText(arg0.getMsg()+"测试测试");
				}
			});*/
			
			break;
		default:
			break;
		}
	}
}
